<?php


namespace app\service;


use app\model\OptionsModel;
use app\tool\SingletonTrait;
use support\bootstrap\Redis;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class OptionsService
{
    // 单例模式工具
    use SingletonTrait;

    /**
     * 获取指定Options设置项值
     * @param $optionName
     * @param $itemName
     * @param $default
     * @return mixed|string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getOptionsConfigItemData($optionName, $itemName, $default = '')
    {
        $configData = $this->getOptionsData($optionName);
        if (!empty($configData) && array_key_exists($itemName, $configData)) {
            return $configData["$itemName"];
        } else {
            return $default;
        }
    }

    /**
     * 获取指定Options设置
     * @param $optionName
     * @return array|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getOptionsData($optionName)
    {
        $resData = OptionsModel::where(['name' => $optionName])->field("config")->find();
        return isset($resData['config']) ? json_decode($resData['config'], true) : [];
    }


    /**
     * getProjectShowStepCategoryListConfig 读取配置
     * @param int $projectId
     * @return array
     */
    public function getProjectShowStepCategoryListConfig(int $projectId = 0): array
    {
        $key = 'project_get_show_step_category_list_config_cache' . $projectId;
        $redisData = Redis::get($key);
        if (!empty($redisData)) {
            return json_decode($redisData, true);
        }
        $param = [];
        if (!empty($projectId)) {
            $param = [
                'param' => [
                    'project_id' => $projectId,
                    'include_step'=>'yes'
                ]
            ];
        }
        $res = teamones_request('teamones-saas', 'project/get_show_step_category_list', $param);
        if (empty($res['data'])) {
            return [];
        }
        Redis::set($key, json_encode($res['data']), 'EX', 60);
        return $res['data'];
    }

    /**
     * GetTaskReviewTagConfig 读取配置
     * @param int $projectId
     * @return array|mixed
     */
    public function getProjectReviewOptions(int $projectId)
    {
        $res = teamones_request('teamones-saas', 'project_review_options/select_project_review_options', [
            'param' => [
                'filter' => [
                    'project_id' => $projectId
                ]
            ]
        ]);
        if (empty($res['data'])) {
            return [];
        }
        return $res['data'];
    }

    /**
     * 获取指定Options设置项值
     * @param $optionName
     * @param array $itemNames
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getOptionsConfigMultiItemData($optionName, array $itemNames): array
    {
        $configData = $this->getOptionsData($optionName);
        $res = array_fill_keys($itemNames, null);
        foreach ($res as $ck => &$cv) {
            if (array_key_exists($ck, $configData)) {
                $cv = $configData["$ck"];
            }
        }
        return $res;
    }

    /**
     * 读取配置显示的项目Id
     * @return mixed
     */
    public function getBiConfigShowProjectIdList()
    {
        $data = teamones_request('teamones-saas', 'bi/project_show_id_list', []);
        return $data['data'];
    }

    /**
     * 读取项目列表配置
     * @param $projectIdList
     * @return array
     */
    public function selectBiProjectConfig($projectIdList)
    {
        $data = teamones_request('teamones-saas', 'bi/select_config', [
            'param' => [
                'filter' => [
                    'id' => ['-in', $projectIdList],
                ],
                'page' => [1, count($projectIdList)],
            ]
        ]);
        return array_column($data['data'], null, 'id');
    }

    /**
     * 读取配置
     * @param $projectId
     * @return mixed
     */
    public function getProjectBIConfig($projectId)
    {
        return teamones_request('teamones-saas', 'bi/find_config', [
            'param' => [
                'filter' => [
                    'id' => $projectId
                ]
            ]
        ])['data'];
    }
}
